package com.example.demospringboot;

import com.example.demospringboot.flow.exec.MyGlobalEventListener;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhaoPin
 * @Date 2024/7/23  16:36
 * @description: 监听器
 */

@SpringBootTest
public class Flowable04Listener {

    @Autowired
    ProcessEngine processEngine;

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    /**
     * 流程部署操作
     */
    @Test
    void deployFlow() throws InterruptedException {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/04-监听器/listener-execution-01.bpmn20.xml")
                .name("监听器")
                .deploy();// 部署的方法
        System.out.println("*******  deploy.getId() = " + deploy.getId());

    }

    /**
     * 启动流程实例
     *  vkaffbzroxqgbghd
     */
    @Test
    void startFlow() throws InterruptedException{
        // 在流程定义表中动态维护
        String processId = "listener-execution-01:2:ad24ccae-4961-11ef-9864-b469218e373e";
        Map<String,Object> map = new HashMap<>();
        // 1.根据流程定义ID启动流程实例
        //map.put("inputArray", Arrays.asList(10,11,12,13,14));
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processId);
        //Thread.sleep(Integer.MAX_VALUE);

        MyGlobalEventListener myGlobalEventListener = new MyGlobalEventListener();
        runtimeService.addEventListener(myGlobalEventListener, FlowableEngineEventType.ACTIVITY_CANCELLED);
    }

    /**
     * 任务的审批
     */
    @Test
    void completeTask() throws InterruptedException {
        Map<String, Object> map = new HashMap<>();
        map.put("skipFlag",true);
        taskService.complete("e47f7c69-4961-11ef-8cf3-b469218e373e",map);
        //Thread.sleep(Integer.MAX_VALUE);
    }

}
